package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.dto.DishDto;
import com.itheima.dto.SetMealDto;
import com.itheima.pojo.*;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetMealDishService;
import com.itheima.service.SetMealService;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetMealController {
    @Autowired
    private SetMealService setMealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private DishService dishService;

    @Autowired
    private SetMealDishService setMealDishService;
    @GetMapping("/page")
    public R page(Integer page, Integer pageSize, String name) {
        log.info("开启套餐查询");

        Page<SetMeal> setMealPage = new Page<>(page, pageSize);
        Page<SetMealDto> setMealDtoPage = new Page<SetMealDto>();

        LambdaQueryWrapper<SetMeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name!=null,SetMeal::getName,name);

        setMealService.page(setMealPage,lqw);
        BeanUtils.copyProperties(setMealPage,setMealDtoPage,"records");
        List<SetMeal> setMealList = setMealPage.getRecords();

        List<SetMealDto> setMealDtoList= setMealList.stream().map(setMeal -> {
            SetMealDto setMealDto = new SetMealDto();
            BeanUtils.copyProperties(setMeal, setMealDto);
            Long categoryId = setMeal.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                String categoryName = category.getName();
                setMealDto.setCategoryName(categoryName);
            }
            return setMealDto;
        }).collect(Collectors.toList());

        setMealDtoPage.setRecords(setMealDtoList);
        return R.success(setMealDtoPage);

    }
    @CacheEvict(value = "setMealCache",allEntries = true)
    @PostMapping()
    public R addSetMealDish(@RequestBody SetMealDto setMealDto){
        log.info("开始新建套餐...");
        setMealService.addMealDish(setMealDto);
        return R.success("新建套餐成功!");
    }

    @PostMapping("/status/{status}")
    public R updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("售停菜品");

        List<SetMeal> setMealList = new ArrayList<>();
        for (Long id : ids) {
            SetMeal setMeal = new SetMeal();
            setMeal.setStatus(status);
            setMeal.setId(id);
            setMealList.add(setMeal);
        }

        setMealService.updateBatchById(setMealList);
        return R.success("售停成功!");

    }


    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        log.info("开始删除");

        LambdaQueryWrapper<SetMeal> lqw = new LambdaQueryWrapper<>();
        lqw.in(SetMeal::getId,ids);
        setMealService.remove(lqw);

        LambdaQueryWrapper<SetMealDish> lqwSMD = new LambdaQueryWrapper<>();
        lqwSMD.in(SetMealDish::getDishId,ids);
        setMealDishService.remove(lqwSMD);
        return R.success("删除成功!");
    }
    @GetMapping("/{id}")
    public R selectById(@PathVariable Long id) {
        log.info("更改菜品信息...");
        SetMealDto setMealDto = setMealService.selectById(id);
        return R.success(setMealDto);
    }


    @PutMapping()
    public R update(@RequestBody SetMealDto setMealDto){
        log.info("更改套餐信息");
        setMealService.updateMeal(setMealDto);
        return R.success("更改信息成功!");
    }

    @GetMapping("/list")
    @Cacheable(value = "setMealCache",key = "#setMeal.categoryId+'_'+#setMeal.status")
    public R list(SetMeal setMeal){
        log.info("查询套餐菜品...");

        LambdaQueryWrapper<SetMeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetMeal::getCategoryId,setMeal.getCategoryId()).eq(SetMeal::getStatus,1);
        List<SetMeal> setMealList = setMealService.list(lqw);
        return R.success(setMealList);
    }

    @GetMapping("/dish/{setMealId}")
    public R  dishDetails(@PathVariable Long setMealId){
        log.info("获取套餐的全部菜品...");
        LambdaQueryWrapper<SetMealDish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SetMealDish::getSetmealId,setMealId);
        List<SetMealDish> setMealDishes = setMealDishService.list(lqw);
        //获取套餐集合

        List<Dish> dishes = setMealDishes.stream().map(new Function<SetMealDish, Dish>() {


            @Override
            public Dish apply(SetMealDish setMealDish) {
                LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
                LambdaQueryWrapper<Dish> lqwDish = lqw.eq(Dish::getId, setMealDish.getDishId());
                Dish dish = dishService.getOne(lqw);
                return dish;
            }
        }).collect(Collectors.toList());


        return R.success(dishes);

    }
}
